package com.xixibaby.pay;

import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.api.BasePayService;
import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.bean.TransferOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.Map;

@Slf4j
public abstract class AbstractPay<T extends BasePayService>
implements Pay{
    @Autowired
    protected T payService;

    @Override
    public Map<String, Object> appOrder(PayOrder order) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        log.info("统一下单参数：[{}],APPID:[{}],notify:[{}]", JSONObject.toJSONString(order), payConfigStorage.getAppid(),payConfigStorage.getNotifyUrl());
        order.setTransactionType(this.getAppTransType());
        return payService.orderInfo(order);
    }

    protected void overConfig(PayConfigStorage payConfigStorage){}


    @Override
    public boolean verify(Map<String, Object> map) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        return payService.verify(map);
    }

    @Override
    public Map<String, Object> query(String tradeNo, String outTradeNo) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        return payService.query(tradeNo,outTradeNo);
    }

    @Override
    public Map<String, Object> getParameter2Map(Map<String, String[]> parameterMap, InputStream is) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        return payService.getParameter2Map(parameterMap,is);
    }

    protected abstract TransactionType getAppTransType() ;

    @Override
    public Map<String, Object> transfer(TransferOrder order) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        return payService.transfer(order);
    }

    @Override
    public Map<String, Object> refund(RefundOrder order) {
        PayConfigStorage payConfigStorage = payService.getPayConfigStorage();
        this.overConfig(payConfigStorage);
        return payService.refund(order);
    }
}
